#include "mylabelclass.h"
#include <QPen>
#include<QPainter>
#include<QLine>
myLabelClass::myLabelClass()
{
    this->lineStartPoint = QPoint(0,0);
    this->lineEndPoint = QPoint(0,0);
    this->lineColor = QColor(Qt::red);
    this->lineSize = 3;
}

//绘制线条
void myLabelClass::paintEvent(QPaintEvent *event)
{
    QLabel::paintEvent(event);//必须有，才能让背景图片显示出来
    QPainter painter(this);
    QPen pen;
    pen.setColor(lineColor);
    pen.setWidth(lineSize);
    painter.setPen(pen);
    painter.drawLines(lines);
//    painter.drawLine(lineStartPoint,lineEndPoint);
}

//鼠标按下
void myLabelClass::mousePressEvent(QMouseEvent *e)
{
    lineStartPoint = e->pos();
    lineEndPoint = e->pos();
    //在图片上绘制
    isPressed = true;
}

//鼠标移动
void myLabelClass::mouseMoveEvent(QMouseEvent *e)
{
    if(isPressed)
    {
        lineEndPoint=e->pos();
        update();
    }
}

//鼠标抬起
void myLabelClass::mouseReleaseEvent(QMouseEvent *e)
{
    lines.append(QLine(lineStartPoint,lineEndPoint));
    isPressed=false;
    update();
}

void myLabelClass::setLineColor(const QColor lineColor)
{
    this->lineColor = lineColor;
}

void myLabelClass::setLineSize(const int lineSize)
{
    this->lineSize = lineSize;
}

QPoint myLabelClass::getStartPoint()
{
    return lineStartPoint;
}

QPoint myLabelClass::getEndPoint()
{
    return lineEndPoint;
}

void myLabelClass::clear()
{
    lineStartPoint = QPoint(0,0);
    lineEndPoint = QPoint(0,0);
    update();
}
